﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace app
{
    class RequestHeader
    {
        byte[] byte_params;

        public RequestCommand Command = RequestCommand.None;

        public byte Jumps = 0;

        public IPEndPoint RemoteEndPoint;

        internal RequestHeader(RequestCommand command)
        {
            Command = command;
        }

        internal static RequestHeader Default()
        {
            return new RequestHeader(RequestCommand.None);
        }

        internal RequestHeader(IPEndPoint remoteEndPoint)
        {
            RemoteEndPoint = remoteEndPoint;
        }

        internal RequestHeader(byte[] parameters)
        {
            byte_params = parameters;

            if(byte_params[0] == 0)
                Command = (RequestCommand)byte_params[Parameters.requestHeaderSize - 1];

            Jumps = parameters[Parameters.requestHeaderSize - 2];
        }

        static internal byte[] ToBytes(RequestCommand command, byte jumps = 0)
        {
            byte[] b = new byte[Parameters.requestHeaderSize];

            b[0] = 0;
            b[Parameters.requestHeaderSize - 2] = (byte)jumps;
            b[Parameters.requestHeaderSize - 1] = (byte)command;

            return b;
        }

        internal byte[] ToBytes()
        {
            if (RemoteEndPoint == null)
                return RequestHeader.ToBytes(this.Command, this.Jumps);
            else
                return Addresses.ToBytes(RemoteEndPoint);
        }
    }

    enum RequestCommand
    {
        None,
        GetPeer,
        GetPeerFromKownPeer,
        AddPeer,
       // Propagate,
    }

}
